package com.hlwxy.ssm.controller;

import com.hlwxy.ssm.model.Page;
import com.hlwxy.ssm.model.SystemUser;
import com.hlwxy.ssm.service.ExcelTextService;
import com.hlwxy.ssm.service.SystemUserService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.Multipart;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private SystemUserService systemUserService;

    @Autowired
    private ExcelTextService excelTextService;
    /**
     * 调整注册用户
     * @return
     */
    @RequestMapping("/doAdd")
    public String doAddUser(){
        return "sys_user/add";
    }


    /**
     * 上传excel
     * @return
     */
    @RequestMapping("/doUpload")
    public String doUpload(){
        return "user_admin/file";
    }



    @RequestMapping("/uploadExcel")
    @ResponseBody
    public Map<String,Object> uploadExcel(MultipartFile file, HttpServletRequest request){
        Map<String,Object> map=new HashMap<String, Object>();
        try {
            InputStream input = file.getInputStream();
            HSSFWorkbook workbook=new HSSFWorkbook(input);
            excelTextService.uploadExcel(workbook);

        }catch (Exception e){
            e.printStackTrace();
            map.put("message","500");
        }
        return map;
    }


    /**
     * 调整注册用户
     * @return
     */
    @RequestMapping("/doUpdate")
    public String doUpdate(String id, Model model){

        SystemUser systemUser=new SystemUser();
        systemUser.setId(Integer.parseInt(id));
        systemUser=systemUserService.getSystemUser(systemUser);
        model.addAttribute("systemUser",systemUser);
        return "sys_user/update";
    }

    /**
     * 返回json数据的 注册方法
     * @param systemUser
     * @return
     */
    @RequestMapping("/register")
    @ResponseBody
    public Map<String,Object> register(SystemUser systemUser, HttpServletRequest request){
        Map<String,Object> map=new HashMap<String, Object>();
        try {
            map.put("message","200");
            systemUserService.register(systemUser);
            String url=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
            systemUserService.sendEmail(url,systemUser.getLoginName(),systemUser.getId()+"");
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","500");
        }
        return map;
    }

    /**
     * 返回json数据的 修改方法
     * @param systemUser
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> update(SystemUser systemUser, HttpServletRequest request){
        Map<String,Object> map=new HashMap<String, Object>();
        try {
            map.put("message","200");
            systemUserService.update(systemUser);
/*            String url=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
            systemUserService.sendEmail(url,systemUser.getLoginName(),systemUser.getId()+"");*/
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","500");
        }
        return map;
    }

    /**
     * 返回json数据的 注册方法
     * @param systemUser
     * @return
     */
    @RequestMapping("/authentication")
    @ResponseBody
    public Map<String,Object> authentication(SystemUser systemUser, HttpServletRequest request){
        Map<String,Object> map=new HashMap<String, Object>();
        try {
            map.put("message","200");
            systemUserService.mailAuthentication(systemUser);
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","500");
        }
        return map;
    }


    /**
     * 数据列表
     * @param page
     * @param pageSize
     * @param pageNumber
     * @return
     */
    @RequestMapping("/data")
    @ResponseBody
    public Map<String,Object> data(Page page,String pageSize,String pageNumber, HttpServletRequest request){
        Map<String,Object> map=new HashMap<String, Object>();
        try {

            map.put("message","200");
            if(null!=pageSize && null!=pageNumber){
                page.setShowCount(Integer.parseInt(pageSize));
                page.setCurrentPage(Integer.parseInt(pageNumber));
            }
           SystemUser systemUser=new SystemUser();
            systemUser.setPage(page);
            map.put("rows",systemUserService.findDemoUserByPage(systemUser));
            map.put("total",page.getTotalResult());
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","500");
        }
        return map;
    }

    @RequestMapping("/index")
    public String userIndex(){
        return "sys_user/index";
    }

   /* *//**
     * 返回json数据的 注册方法
     * @param systemUser
     * @return
     *//*
    @RequestMapping("/data")
    @ResponseBody
    public Map<String,Object> data2(SystemUser systemUser, HttpServletRequest request){
        Map<String,Object> map=new HashMap<String, Object>();
        try {
            int pageSize=5;
            map.put("message","200");
            Integer totalCount=systemUserService.findSystemUserCount(systemUser);
            map.put("totalPage",totalCount/pageSize+1);

            systemUser.setPageSize(pageSize);
            map.put("list",systemUserService.findSystemUserByPage(systemUser));
        }catch (Exception e){
            e.printStackTrace();
            map.put("message","500");
        }
        return map;
    }*/

    /**
     *
     * @param request
     * @return
     */
    @RequestMapping("/exportExcel")
    public void exportExcel(HttpServletRequest request,HttpServletResponse response){
        try {
            HSSFWorkbook workbook=excelTextService.exportExcel();
            response.reset();
            response.setContentType("application/octet-stream; charset=utf-8");
            response.setHeader("Content-Disposition", "attachment; filename=" + new Date().getTime() + ".xlsx");
            workbook.write(response.getOutputStream());
        }catch (Exception e){
            e.printStackTrace();
        }

    }

}
